package com.qifan.alb2b.action.back;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.domain.AdminInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.AdminInfoService;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;
import com.qifan.alb2b.util.Stringer;

@Controller
@RequestMapping("/admin/admin")
public class AdminManageController{
	private AdminInfoService adminInfoService;

	public void setAdminInfoService(AdminInfoService adminInfoService) {
		this.adminInfoService = adminInfoService;
	}
	
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm,AdminInfoDomain uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		return adminInfoService.query(dgm,uqp);
	}
	
	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(AdminInfoDomain adminInfoDomain){
		Map<String, Object> map=new HashMap<String, Object>();
		adminInfoDomain.setAdminId(ID.next()+"");//设置唯一ID
		adminInfoDomain.setStatus(Constants.NOMARL);
		adminInfoDomain.setAdminPwd(Stringer.md5(Constants.PASSWORD).substring(0,30));
		adminInfoDomain.setCreateTime(new Date());//设置创建日期
		adminInfoService.add(adminInfoDomain);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(AdminInfoDomain adminInfoDomain){
		AdminInfoDomain adminInfo=adminInfoService.getAdminById(adminInfoDomain.getAdminId());
		Map<String, Object> map=new HashMap<String, Object>();
		adminInfoDomain.setCreateTime(new Date());//设置创建日期
		adminInfoDomain.setAdminPwd(adminInfo.getAdminPwd());
		adminInfoService.update(adminInfoDomain);
		map.put("success", true);	
		return map;
	}
	
	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("uid") String[] ids){
		Map<String, Object> map=new HashMap<String, Object>();
		adminInfoService.delete(ids);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/updateStatus.adm")
	@ResponseBody
	public Map<String, Object> updateStatus(@RequestParam("uid") String[] ids,@RequestParam("status") String status){
		Map<String, Object> map=new HashMap<String, Object>();
		adminInfoService.updateStatus(ids,status);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/updatePassword.adm")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request,String password,String password1){
		AdminInfoDomain admin = (AdminInfoDomain)request.getSession().getAttribute(Constants.SESSION_ADMIN);
		password=Stringer.md5(password).substring(0,30);
		password1=Stringer.md5(password1).substring(0,30);
		

		if(!password.equals(admin.getAdminPwd())){
			throw new BusinessException("原密码输入错误！");
		}
		
		Map<String, Object> map=new HashMap<String, Object>();
		admin.setAdminPwd(password1);
		
		adminInfoService.update(admin);
		request.getSession().setAttribute("ADMIN",admin);
		map.put("success", true);	
		return map;
	}
	
	@RequestMapping("/resetPassword.adm")
	@ResponseBody
	public Map<String, Object> reset(HttpServletRequest request){
		AdminInfoDomain admin = (AdminInfoDomain)request.getSession().getAttribute(Constants.SESSION_ADMIN);
		
		Map<String, Object> map=new HashMap<String, Object>();
		admin.setAdminPwd(Stringer.md5(Constants.PASSWORD).substring(0,30));
		
		adminInfoService.update(admin);
		request.getSession().setAttribute("ADMIN",admin);
		map.put("success", true);	
		return map;
	}
	
	
	
}
